package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class ChoosePicActivity extends BaseActivity implements View.OnClickListener {
    private ImageView iv_choose_pic;
    private ImageView timegood_back;
    private TextView tv_sure;
    private ImageView iv_choose;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_pic);
        iv_choose_pic = ((ImageView) findViewById(R.id.iv_choose_pic));
        timegood_back = ((ImageView) findViewById(R.id.timegood_back));
        tv_sure = ((TextView) findViewById(R.id.tv_sure));
        iv_choose = ((ImageView) findViewById(R.id.iv_choose));

        timegood_back.setOnClickListener(this);
        tv_sure.setOnClickListener(this);
        iv_choose.setOnClickListener(this);
        iv_choose_pic.setOnClickListener(this);
        Intent intent = getIntent();
        if (intent.getIntExtra("position", 0) >= 0 && intent.getStringExtra("local3") == null && intent.getStringExtra("back") == null) {
            SelectedImg.bitmapListBackup.clear();
            SelectedImg.choosePicBackup.clear();
            position = intent.getIntExtra("position", 0);
            SharedPreferences sp = getSharedPreferences("position", MODE_PRIVATE);
            SharedPreferences.Editor edit = sp.edit();
            edit.putInt("position", position);
            edit.commit();
            iv_choose_pic.setImageBitmap(SelectedImg.bitmaplist.get(this.position));
            for (int i = 0; i < SelectedImg.bitmaplist.size(); i++) {
                SelectedImg.bitmapListBackup.add(SelectedImg.bitmaplist.get(i));
                SelectedImg.choosePicBackup.add(SelectedImg.choosepic.get(i));
            }
        } else if (TextUtils.equals(intent.getStringExtra("local3"), "local3")) {
            select();
        } else {
            SharedPreferences sp = getSharedPreferences("position", MODE_PRIVATE);
            position = sp.getInt("position", 0);
            iv_choose_pic.setImageBitmap(SelectedImg.bitmaplist.get(position));
        }
    }

    private void select() {
        Bitmap bitmap;
        if (!SelectedImg.mSelectedImage.get(0).startsWith("http://")) {
            bitmap = returnBitmap(SelectedImg.mSelectedImage.get(0));
        } else {
            String s = SelectedImg.seleBitmap.get(SelectedImg.mSelectedImage.get(0));
            bitmap = returnBitmap(s);
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int cropwidth;
        int cropheight;
        if (width >= height) {
            cropheight = height;
            cropwidth = height;
        } else {
            cropwidth = width;
            cropheight = width;
        }
        Bitmap cropBitmap = Bitmap.createBitmap(bitmap, 0, 0, cropwidth, cropheight);
        iv_choose_pic.setImageBitmap(cropBitmap);
        List<Bitmap> list = new ArrayList<>();
        for (int i = 0; i < SelectedImg.bitmaplist.size(); i++) {
            list.add(SelectedImg.bitmaplist.get(i));
        }
        SelectedImg.bitmaplist.clear();
        SharedPreferences sp = getSharedPreferences("position", MODE_PRIVATE);
        position = sp.getInt("position", 0);
        for (int i = 0; i < list.size(); i++) {
            if (i == position) {
                SelectedImg.bitmaplist.add(cropBitmap);
            } else {
                SelectedImg.bitmaplist.add(list.get(i));
            }
        }
        List<String> listChoose = new ArrayList<>();
        for (int i = 0; i < SelectedImg.choosepic.size(); i++) {
            listChoose.add(SelectedImg.choosepic.get(i));
        }
        SelectedImg.choosepic.clear();
        for (int i = 0; i < listChoose.size(); i++) {
            if (i == position) {
                SelectedImg.choosepic.add(SelectedImg.mSelectedImage.get(0));
            } else {
                SelectedImg.choosepic.add(listChoose.get(i));
            }
        }
        SelectedImg.mSelectedImage.clear();
//        SharedPreferences.Editor edit = sp.edit();
//        edit.putInt("position", 0);
//        edit.commit();
    }

    private Bitmap returnBitmap(String path) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = false;
        newOpts.inPurgeable = true;
        newOpts.inInputShareable = true;
        newOpts.inSampleSize = 2;
        newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
        return BitmapFactory.decodeFile(path, newOpts);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.timegood_back:
                SelectedImg.bitmaplist.clear();
                SelectedImg.choosepic.clear();
                for (int i = 0; i < SelectedImg.bitmapListBackup.size(); i++) {
                    SelectedImg.bitmaplist.add(SelectedImg.bitmapListBackup.get(i));
                    SelectedImg.choosepic.add(SelectedImg.choosePicBackup.get(i));
                }
                SelectedImg.choosePicBackup.clear();
                SelectedImg.bitmapListBackup.clear();
                startActivity(new Intent(this, TimeGoodsLomoActivity.class));
                finish();
                break;
            case R.id.tv_sure:
                startActivity(new Intent(this, TimeGoodsLomoActivity.class));
                finish();
                break;
            case R.id.iv_choose:
                SharedPreferences sp = getSharedPreferences("ChoosePicActivity", MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("ChoosePicActivity", "ChoosePicActivity");
                edit.putBoolean("isReplace", true);
                edit.commit();
                startActivity(new Intent(this, TimeGoodsAlbumActivity.class));
//                finish();
                break;
            //调整图片的显示区域
            case R.id.iv_choose_pic:
                Log.d("yuhao", "position=" + position);
                Log.d("yuhao", "SelectedImg.choosepic.get(position)=" + SelectedImg.choosepic.get(position));
                Intent intent = new Intent();
                intent.setAction("com.android.camera.action.CROP");
                if (SelectedImg.choosepic.get(position).startsWith("http://")) {
                    intent.setDataAndType(Uri.parse(SelectedImg.choosepic.get(position)), "image/*");// mUri是已经选择的图片Uri
                } else {
                    intent.setDataAndType(Uri.parse("file://" + SelectedImg.choosepic.get(position)), "image/*");// mUri是已经选择的图片Uri
                }
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 1000);// 裁剪框比例
                intent.putExtra("aspectY", 1001);
                intent.putExtra("outputX", 300);// 输出图片大小
                intent.putExtra("outputY", 300);
                intent.putExtra("return-data", true);
                ChoosePicActivity.this.startActivityForResult(intent, 200);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 200 && resultCode == RESULT_OK) {
            // 拿到剪切数据
            Bitmap bmap = data.getParcelableExtra("data");
            iv_choose_pic.setImageBitmap(bmap);
            List<Bitmap> list = new ArrayList<>();
            for (int i = 0; i < SelectedImg.bitmaplist.size(); i++) {
                list.add(SelectedImg.bitmaplist.get(i));
            }
            SelectedImg.bitmaplist.clear();
            SharedPreferences sp = getSharedPreferences("position", MODE_PRIVATE);
            position = sp.getInt("position", 0);
            Log.d("yuhao", "position=" + position);
            for (int i = 0; i < list.size(); i++) {
                if (i == position) {
                    SelectedImg.bitmaplist.add(bmap);
                } else {
                    SelectedImg.bitmaplist.add(list.get(i));
                }
            }
//            SelectedImg.choosepic.clear();
            SelectedImg.mSelectedImage.clear();
//            SharedPreferences.Editor edit = sp.edit();
//            edit.putInt("position", 0);
//            edit.commit();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        Log.d("yuhao", "onDestroy---sss");
//        SharedPreferences sp = getSharedPreferences("ChoosePicActivity", MODE_PRIVATE);
//        SharedPreferences.Editor edit = sp.edit();
//        edit.putBoolean("isReplace", false);
//        edit.commit();
        super.onDestroy();
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
